package com.aimosheng.main.dialog;

import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tencent.imsdk.ext.message.TIMBatchOprDetailInfo;
import com.tencent.imsdk.ext.message.TIMSendMsgToMultiUserCallback;
import com.aimosheng.common.CommonAppConfig;
import com.aimosheng.common.bean.UserBean;
import com.aimosheng.common.dialog.AbsDialogFragment;
import com.aimosheng.common.http.HttpCallback;
import com.aimosheng.common.utils.DpUtil;
import com.aimosheng.common.utils.ToastUtil;
import com.aimosheng.common.utils.WordUtil;
import com.aimosheng.im.utils.ImMessageUtil;
import com.aimosheng.main.R;
import com.aimosheng.main.adapter.RecommendAdapter;
import com.aimosheng.main.bean.RecommendUserBean;
import com.aimosheng.main.http.MainHttpConsts;
import com.aimosheng.main.http.MainHttpUtil;

import java.util.List;

/**
 * Des:打招呼
 * <p>
 * Create by W_X on 2021/3/1.4:28 PM.
 */
public class RecommendDialogFragment extends AbsDialogFragment implements View.OnClickListener {

    private RecyclerView mRvUser;
    private RecommendAdapter mAdapter;
    private TextView mTvName;
    private TextView mTvTips;

    private int mPage = 1;
    private int mTotalPage = 1;
    private String mSendMsg;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_recommend;
    }

    @Override
    protected int getDialogStyle() {
        return R.style.dialog;
    }

    @Override
    protected boolean canCancel() {
        return false;
    }

    @Override
    protected void setWindowAttributes(Window window) {
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = DpUtil.dp2px(260);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        findViewById(R.id.btn_close).setOnClickListener(this);
        findViewById(R.id.btn_send_msg).setOnClickListener(this);
        findViewById(R.id.btn_change).setOnClickListener(this);
        mRvUser = (RecyclerView) findViewById(R.id.rv_user);
        mTvName = (TextView) findViewById(R.id.tv_name);
        mTvTips = (TextView) findViewById(R.id.tv_tips);
        mRvUser.setLayoutManager(new GridLayoutManager(
                mContext, 3));
        mAdapter = new RecommendAdapter();
        mRvUser.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mAdapter.updateData(position);
            }
        });
        getRecommendList(1);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_close) {
            dismiss();
        } else if (i == R.id.btn_send_msg) {
            sendMsg();
        } else if (i == R.id.btn_change) {
            ++mPage;
            if (mPage == mTotalPage) {
                getRecommendList(mPage);
                ToastUtil.show(R.string.all_show_finish);
                mPage = 0;
            } else {
                getRecommendList(mPage);
            }
        }
    }

    private void getRecommendList(int p) {
        MainHttpUtil.getRecommendList(p, new HttpCallback() {
            @Override
            public void onSuccess(int code, String msg, String[] info) {
                if (code == 0) {
                    JSONObject jsonObject = JSON.parseObject(info[0]);
                    mTotalPage = jsonObject.getIntValue("pages");
                    mSendMsg = jsonObject.getString("sayhelloinfo");
                    String mTips = jsonObject.getString("recommendinfo");
                    UserBean userBean = CommonAppConfig.getInstance().getUserBean();
                    if (userBean != null) {
                        mTvName.setText(String.format(WordUtil.getString(R.string.Hi), userBean.getUserNiceName()));
                    }
                    mTvTips.setText(mTips);
                    String mRecommendList = jsonObject.getString("recommendlist");
                    List<RecommendUserBean> userBeans = JSON.parseArray(mRecommendList, RecommendUserBean.class);
                    if (userBeans != null && userBeans.size() > 0) {
                        mAdapter.setNewData(userBeans);
                    } else {
                        dismiss();
                    }
                } else {
                    dismiss();
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        MainHttpUtil.cancel(MainHttpConsts.GET_RECOMMEND_LIST);
        super.onDestroy();
    }

    private void sendMsg() {
        List<String> mUids = mAdapter.getAllCheckedUids();
        if (mUids == null || mUids.size() == 0) {
            ToastUtil.show(WordUtil.getString(R.string.choose_user));
            return;
        }
        ImMessageUtil.getInstance().sendMessageToMultiUsers(mUids, mSendMsg, new TIMSendMsgToMultiUserCallback() {
            @Override
            public void onSuccess() {
                ToastUtil.show(WordUtil.getString(R.string.send_success));
                dismiss();
            }

            @Override
            public void onError(int code, String err, TIMBatchOprDetailInfo info) {
                ToastUtil.show(WordUtil.getString(R.string.send_fail));
            }
        });
    }
}
